﻿using Autodesk.AutoCAD.Geometry;
using FDEEPublicFunc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FDEEInterface
{
    public class BoxGirderBar
    {

        static double n = 2;//抛物线n次方
        static double c = 0;//高差  
        static double h = 0;
        static double k = 0;
        static double x = 0;//距离差

        /// <summary>
        /// 获取顶板坐标点
        /// </summary>
        /// <param name="paraList"></param>
        /// <param name="pt"></param>
        /// <param name="stWidth"></param>
        /// <param name="endWidth"></param>
        /// <param name="isRight"></param>
        /// <returns></returns>
        public static  List<Point2d> GetTopList(List<string> paraList,Point2d pt,double stWidth, double endWidth,string topSize,bool isRight=true)
        {
            List<Point2d> plist = new List<Point2d>();
            if (isRight)
            {
                double w = double.Parse(topSize.Split('*')[0]);
                Point2d ptItem = pt.Convert2D(w);
                plist.Add(ptItem);
                ptItem = pt.Convert2D(stWidth);
                plist.Add(ptItem);
                foreach (string item in paraList)
                {
                    double num = 1;
                    double.TryParse(item.Split('*')[0].ToString(), out num);
                    double width = 1;
                    double.TryParse(item.Split('*')[1].ToString(), out width);
                    for (int i = 0; i < num; i++)
                    {
                        Point2d pT = plist[plist.Count - 1].Convert2D(width);
                        plist.Add(pT);
                    }
                }
                plist.Add(plist[plist.Count - 1].Convert2D(endWidth));
            }
            else
            {
                double w = double.Parse(topSize.Split('*')[0]);
                Point2d ptItem = pt.Convert2D(-w);
                plist.Add(ptItem);
                ptItem = pt.Convert2D(-stWidth);
                plist.Add(ptItem);
                foreach (string item in paraList)
                {
                    double num = 1;
                    double.TryParse(item.Split('*')[0].ToString(), out num);
                    double width = 1;
                    double.TryParse(item.Split('*')[1].ToString(), out width);
                    for (int i = 0; i < num; i++)
                    {
                        Point2d pT = plist[plist.Count - 1].Convert2D(-width);
                        plist.Add(pT);
                    }
                }
                plist.Add(plist[plist.Count - 1].Convert2D(-endWidth));
            }
            return plist;
        }

        /// <summary>
        /// 计算n次抛物线参数
        /// </summary>
        /// <param name="paraList"></param>
        /// <param name="stWidth"></param>
        /// <param name="endWidth"></param>
        /// <param name="stHeight"></param>
        /// <param name="endheight"></param>
        /// <param name="topSize"></param>
        /// <param name="allW"></param>
        public static void CalculatPara(List<string> paraList, double stWidth, double stHeight, double endheight, string topSize,out double allW)
        {
            c = -endheight;//  
            x = 0;//距离差
            double w = double.Parse(topSize.Split('*')[0]);
            foreach (string item in paraList)
            {
                double num = 1;
                double.TryParse(item.Split('*')[0].ToString(), out num);
                double width = 1;
                double.TryParse(item.Split('*')[1].ToString(), out width);
                x += num * width;
            }
            x += stWidth + 125;
            k = (-stHeight - c) / (Math.Pow(-x+160, n));
            allW = -x + 160;
        }
      
        /// <summary>
        /// 获取底板/梁高抛物线点
        /// </summary>
        /// <param name="paraList"></param>
        /// <param name="pt"></param>
        /// <param name="stWidth"></param>
        /// <param name="endWidth"></param>
        /// <param name="stHeight"></param>
        /// <param name="endheight"></param>
        /// <param name="isRight"></param>
        /// <returns></returns>
        public static List<Point2d> GetParaCurveList(List<string> paraList, Point2d pt, double stWidth, double endWidth, double stHeight, double endheight, string topSize, bool isRight = true)
        {
            double allW = 0;
            CalculatPara(paraList, stWidth, stHeight, endheight, topSize,out allW);         
            List<Point2d> plist = new List<Point2d>();
            double y = 0;
            double w = double.Parse(topSize.Split('*')[0]);
            if (isRight)
            {
                double widthAll = w;
                CalculatParaCurvePoint(allW+widthAll, k, n, c, out y);
                //CalculatParaCurvePointNew(widthAll, k, n, c, out y);
                Point2d ptItem = pt.Convert2D(w, y);
                plist.Add(ptItem);
                widthAll = stWidth-35;
                CalculatParaCurvePoint(allW + widthAll, k, n, c, out y);
                //CalculatParaCurvePointNew(widthAll, k, n, c, out y);
                ptItem = pt.Convert2D(stWidth, y);
                plist.Add(ptItem);

                foreach (string item in paraList)
                {
                    double num = 1;
                    double.TryParse(item.Split('*')[0].ToString(), out num);
                    double width = 1;
                    double.TryParse(item.Split('*')[1].ToString(), out width);
                    y = 0;

                    for (int i = 0; i < num; i++)
                    {
                        widthAll += width;
                        CalculatParaCurvePoint(allW + widthAll, k, n, c, out y);
                        //CalculatParaCurvePointNew(widthAll, k, n, c, out y);
                        Point2d pT = new Point2d(plist[plist.Count - 1].Convert2D(width, 0).X, pt.Y + y);
                        plist.Add(pT);
                    }
                }
                //widthAll += endWidth;
                //CalculatParaCurvePoint(allW + widthAll, k, n, c, out y);
                //CalculatParaCurvePointNew(widthAll, k, n, c, out y);
                Point2d pTE = new Point2d(plist[plist.Count - 1].Convert2D(endWidth, 0).X, pt.Y - endheight);
                plist.Add(pTE);
            }
            else
            {
                //Point2d ptItem = pt.Convert2D(-stWidth);
                //plist.Add(ptItem);
                Point2d ptItem = pt.Convert2D(-w,-stHeight);
                plist.Add(ptItem);
                double widthAll = stWidth-35;
                CalculatParaCurvePoint(allW + widthAll, k, n, c, out y);
                //CalculatParaCurvePointNew(widthAll, k, n, c, out y);
                ptItem = pt.Convert2D(-stWidth, y);
                plist.Add(ptItem);
                foreach (string item in paraList)
                {
                    double num = 1;
                    double.TryParse(item.Split('*')[0].ToString(), out num);
                    double width = 1;
                    double.TryParse(item.Split('*')[1].ToString(), out width);
                    for (int i = 0; i < num; i++)
                    {
                        widthAll += width;
                        CalculatParaCurvePoint(allW + widthAll, k, n, c, out y);
                        //CalculatParaCurvePointNew(widthAll, k, n, c, out y);
                        Point2d pT = new Point2d(plist[plist.Count - 1].Convert2D(-width).X, pt.Y+y);
                        plist.Add(pT);
                    }
                }
                //widthAll += -endWidth;
                //CalculatParaCurvePoint(allW + widthAll, k, n, c, out y);
                //CalculatParaCurvePointNew(widthAll, k, n, c, out y);
                Point2d pTE = new Point2d(plist[plist.Count - 1].Convert2D(-endWidth, 0).X, pt.Y - endheight);
                plist.Add(pTE);
                //plist.Add(plist[plist.Count - 1].Convert2D(-endWidth));
            }
            return plist;
        }

        public static void CalculatParaCurvePoint(double x,double k,double n,double c ,out double y)
        {
            y = k * Math.Pow(x, n) + c;
        }

 
    }
}
